package com.bole.controller;


import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.brand.DicBrandDto;
import com.bole.dto.brand.TbCarSeriesDto;
import com.bole.dto.car.TbCarDto;
import com.bole.dto.specdetails.TbCarSpecDetailsDto;
import com.bole.dto.user.DicCarSpecDto;
import com.bole.dto.user.UserDto;
import com.bole.feign.CarSeriesFeign;
import com.bole.feign.CarSpecDetailsFeign;
import com.bole.feign.CarSpecFeign;
import com.bole.feign.DicBrandFeign;
import com.bole.feign.reception.CarFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("sellCar")
@Api(value = "卖车api")
public class SellingCarController {

    @Autowired
    private CarFeign carFeign;

    @Autowired
    private DicBrandFeign brandFeign;

    @Autowired
    private CarSeriesFeign carSeriesFeign;

   @Autowired
   private CarSpecDetailsFeign carSpecDetailsFeign;

   @Autowired
   private CarSpecFeign carSpecFeign;

    @RequestMapping("toSellCar")
    public String toSellCar() {
        UserDto user = (UserDto)SecurityUtils.getSubject().getPrincipal();
        //先判断是否登录
       if(user == null || "".equals(user)) {
            return "notlogging";
        }else{
            return "sellCar";
       }
    }


    @PostMapping("saveCar")
    @ApiOperation(value = "卖车", notes = "卖车")
    @ApiImplicitParam(name = "carDto", value = "广告分类对象", required = true, dataTypeClass = TbCarDto.class)
    @ResponseBody
    public Result saveCar(@RequestBody TbCarDto carDto) {
        UserDto user = (UserDto)SecurityUtils.getSubject().getPrincipal();
            //如果登录了
            //获取手机号
            carDto.setSellPhone(user.getTelephone());
            //获取卖家id
            carDto.setSellUserId(user.getId());
            carFeign.sellCar(carDto);
            return Result.response(ResultEnum.OK);
    }

    //查询品牌
    @GetMapping("getBrandInfo")
    @ApiOperation(value = "查询品牌", notes = "查询品牌")
    @ResponseBody
    public Result getBrandInfo() {
        List<DicBrandDto> brandInfo = brandFeign.getBrandInfo();
        return Result.response(ResultEnum.OK).setData(brandInfo);
    }

    //通过品牌id查询车系
    @PostMapping("selectByBrandId")
    @ResponseBody
    @ApiOperation(value = "通过品牌id查询", notes = "通过品牌id查询")
    @ApiImplicitParam(name = "brandId", value = "品牌id", required = true, dataTypeClass = Long.class)
    public Result selectByBrandId(@RequestParam("brandId") Long brandId){
        List<TbCarSeriesDto> tbCarSeriesDtos = carSeriesFeign.selectByBrandId(brandId);
        return Result.response(ResultEnum.OK).setData(tbCarSeriesDtos);
    }

    //查询所有规格
    @GetMapping("getSpecInfo")
    @ApiOperation(value = "查询品牌", notes = "查询品牌")
    @ResponseBody
    public Result getSpecInfo(){
        List<DicCarSpecDto> specInfo = carSpecFeign.getSpecInfo();
        return Result.response(ResultEnum.OK).setData(specInfo);
    }

    //根据规格id查询规格详情
    @PostMapping("selectBySpecId")
    @ResponseBody
    @ApiOperation(value = "通过规格id查询", notes = "通过规格id查询")
    @ApiImplicitParam(name = "specId", value = "品牌id", required = true, dataTypeClass = Long.class)
    public Result selectBySpecId(@RequestParam("specId") Long specId){
        List<TbCarSpecDetailsDto> tbCarSpecDetailsDtos = carSpecDetailsFeign.selectBySpecId(specId);
        return Result.response(ResultEnum.OK).setData(tbCarSpecDetailsDtos);
    }

}
